package ac.svn.accesscontrol.ui.panel;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.ui.JPanel;
import ac.svn.accesscontrol.ui.model.ThreadedListModel;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

public class UserGroupEditorPanelI extends JPanel {
    

    public interface ExportedComponents {
	// Variable belonging to user group membership panel.
	public static final String C_UGM_PANEL = "C_UGM_PANEL";
	public static final String C_UGM_AVL_GRP_LIST_VIEW = "C_UGM_AVL_GRP_LIST_VIEW";
	public static final String C_UGM_AVL_GRP_LIST_MODEL = "C_UGM_AVL_GRP_LIST_MODEL";
	public static final String C_UGM_ASND_PANEL = "C_UGM_ASND_PANEL";
    }
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JScrollPane scrollPaneAvlGrps;
    private JLabel lblGroupCount;
    private JLabel lblUser;
    private JLabel lblIcon;
    private JPanel pnlGroups;
    private JButton btnAdd;
    private JButton btnDeleteGroup;
    private JButton editAddGroup;
    private JButton btnAddGroup;
    private ThreadedListModel listModel;
    private JList listAvailableGroups;
    
    public UserGroupEditorPanelI() {

	
	Border border = UIUtility.setAllShadowedBorder(null);
	
	JPanel panel = new JPanel();
	JPanel basePanel = new JPanel();
	basePanel.setBackground(Color.WHITE);
	GroupLayout groupLayout = new GroupLayout(this);
	groupLayout.setHorizontalGroup(
		groupLayout.createParallelGroup(Alignment.LEADING)
		.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
			.addContainerGap()
			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(basePanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
				.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE))
				.addContainerGap())
	);
	groupLayout.setVerticalGroup(
		groupLayout.createParallelGroup(Alignment.LEADING)
		.addGroup(groupLayout.createSequentialGroup()
			.addContainerGap()
			.addComponent(panel, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
			.addPreferredGap(ComponentPlacement.RELATED)
			.addComponent(basePanel, GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
			.addContainerGap())
	);

	scrollPaneAvlGrps = new JScrollPane();

	JScrollPane userGroupsScrPane = new JScrollPane();
	userGroupsScrPane.setBorder(border);

	JLabel lblAvailableGroups = new JLabel("Available Groups");
	lblAvailableGroups.setOpaque(true);
	lblAvailableGroups.setBackground(Color.WHITE);

	JLabel lblNewLabel_3 = new JLabel("<HTML><i>User is member of below listed groups</i></HTML>");

	btnAdd = new JButton("+");

	editAddGroup = new JButton(Utility.getImageIcon(IResources.LOC_ICN_COMMON_EDIT_S));
	btnAddGroup = new JButton(Utility.getImageIcon(IResources.LOC_ICN_COMMON_ADD_S));
	btnDeleteGroup = new JButton(Utility.getImageIcon(IResources.LOC_ICN_COMMON_DELETE_S));
	
	GroupLayout gl_basePanel = new GroupLayout(basePanel);
	gl_basePanel.setHorizontalGroup(
		gl_basePanel.createParallelGroup(Alignment.LEADING)
			.addGroup(gl_basePanel.createSequentialGroup()
				.addContainerGap()
				.addGroup(gl_basePanel.createParallelGroup(Alignment.LEADING, false)
					.addComponent(btnAddGroup, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(btnDeleteGroup, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(editAddGroup, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addGroup(gl_basePanel.createParallelGroup(Alignment.TRAILING, false)
					.addGroup(gl_basePanel.createSequentialGroup()
						.addComponent(lblAvailableGroups)
						.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(btnAdd))
					.addComponent(scrollPaneAvlGrps, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(gl_basePanel.createParallelGroup(Alignment.LEADING)
					.addComponent(lblNewLabel_3, GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
					.addComponent(userGroupsScrPane, GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))
				.addContainerGap())
	);
	gl_basePanel.setVerticalGroup(
		gl_basePanel.createParallelGroup(Alignment.TRAILING)
			.addGroup(gl_basePanel.createSequentialGroup()
				.addContainerGap()
				.addGroup(gl_basePanel.createParallelGroup(Alignment.TRAILING)
					.addGroup(gl_basePanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(btnAdd))
					.addComponent(lblAvailableGroups))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addGroup(gl_basePanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_basePanel.createSequentialGroup()
						.addGap(2)
						.addGroup(gl_basePanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(scrollPaneAvlGrps, GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
							.addGroup(gl_basePanel.createSequentialGroup()
								.addComponent(btnAddGroup)
								.addGap(1)
								.addComponent(editAddGroup)
								.addGap(2)
								.addComponent(btnDeleteGroup))))
					.addComponent(userGroupsScrPane, GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE))
				.addContainerGap())
	);

	pnlGroups = new JPanel();
	pnlGroups.setBorder(new EmptyBorder(0,0,0,0));
	userGroupsScrPane.setViewportView(pnlGroups);
	pnlGroups.setLayout(new GridLayout(50, 1, 0, 0));

	listAvailableGroups = new JList();
	listModel = new ThreadedListModel();
	listAvailableGroups.setModel(listModel);
	
	scrollPaneAvlGrps.setViewportView(listAvailableGroups);
	scrollPaneAvlGrps.setBorder(border);
	basePanel.setLayout(gl_basePanel);

	lblIcon = new JLabel("");
	lblIcon.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));

	lblUser = new JLabel("User");

	lblGroupCount = new JLabel("Groups(0)");
	lblGroupCount.setHorizontalAlignment(SwingConstants.RIGHT);
	lblGroupCount.setHorizontalTextPosition(SwingConstants.RIGHT);
	GroupLayout gl_panel = new GroupLayout(panel);
	gl_panel.setHorizontalGroup(
		gl_panel.createParallelGroup(Alignment.LEADING)
		.addGroup(gl_panel.createSequentialGroup()
			.addComponent(lblIcon, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
			.addPreferredGap(ComponentPlacement.RELATED)
			.addComponent(lblUser, GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
			.addPreferredGap(ComponentPlacement.UNRELATED)
			.addComponent(lblGroupCount, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
			.addContainerGap())
	);
	gl_panel.setVerticalGroup(
		gl_panel.createParallelGroup(Alignment.TRAILING)
		.addComponent(lblIcon, GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
		.addGroup(gl_panel.createSequentialGroup()
			.addGap(11)
			.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
				.addComponent(lblGroupCount, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
				.addComponent(lblUser, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
				.addContainerGap())
	);
	panel.setLayout(gl_panel);
	setLayout(groupLayout);
	
    }

    public void resetData() {
	pnlGroups.removeAll();
	lblGroupCount.setText("Group(s): 0 ");
	lblUser.setText("User Name");
	listModel.clear();
	SwingUtilities.invokeLater(new Runnable() {
	    @Override
	    public void run() {
		SwingUtilities.updateComponentTreeUI(listAvailableGroups);
	    }
	});
    }


    /**
     * @return the scrollPaneAvlGrps
     */
    public JScrollPane getScrollPaneAvlGrps() {
        return scrollPaneAvlGrps;
    }


    /**
     * @param scrollPaneAvlGrps the scrollPaneAvlGrps to set
     */
    public void setScrollPaneAvlGrps(JScrollPane scrollPaneAvlGrps) {
        this.scrollPaneAvlGrps = scrollPaneAvlGrps;
    }


    /**
     * @return the lblGroupCount
     */
    public JLabel getLblGroupCount() {
        return lblGroupCount;
    }


    /**
     * @param lblGroupCount the lblGroupCount to set
     */
    public void setLblGroupCount(JLabel lblGroupCount) {
        this.lblGroupCount = lblGroupCount;
    }


    /**
     * @return the lblUser
     */
    public JLabel getLblUser() {
        return lblUser;
    }


    /**
     * @param lblUser the lblUser to set
     */
    public void setLblUser(JLabel lblUser) {
        this.lblUser = lblUser;
    }


    /**
     * @return the lblIcon
     */
    public JLabel getLblIcon() {
        return lblIcon;
    }


    /**
     * @param lblIcon the lblIcon to set
     */
    public void setLblIcon(JLabel lblIcon) {
        this.lblIcon = lblIcon;
    }


    /**
     * @return the pnlGroups
     */
    public JPanel getPnlGroups() {
        return pnlGroups;
    }


    /**
     * @param pnlGroups the pnlGroups to set
     */
    public void setPnlGroups(JPanel pnlGroups) {
        this.pnlGroups = pnlGroups;
    }


    /**
     * @return the btnAdd
     */
    public JButton getBtnAdd() {
        return btnAdd;
    }


    /**
     * @param btnAdd the btnAdd to set
     */
    public void setBtnAdd(JButton btnAdd) {
        this.btnAdd = btnAdd;
    }


    /**
     * @return the btnDeleteGroup
     */
    public JButton getBtnDeleteGroup() {
        return btnDeleteGroup;
    }


    /**
     * @param btnDeleteGroup the btnDeleteGroup to set
     */
    public void setBtnDeleteGroup(JButton btnDeleteGroup) {
        this.btnDeleteGroup = btnDeleteGroup;
    }


    /**
     * @return the editAddGroup
     */
    public JButton getEditAddGroup() {
        return editAddGroup;
    }


    /**
     * @param editAddGroup the editAddGroup to set
     */
    public void setEditAddGroup(JButton editAddGroup) {
        this.editAddGroup = editAddGroup;
    }


    /**
     * @return the btnAddGroup
     */
    public JButton getBtnAddGroup() {
        return btnAddGroup;
    }


    /**
     * @param btnAddGroup the btnAddGroup to set
     */
    public void setBtnAddGroup(JButton btnAddGroup) {
        this.btnAddGroup = btnAddGroup;
    }


    /**
     * @return the listModel
     */
    public ThreadedListModel getListModel() {
        return listModel;
    }


    /**
     * @param listModel the listModel to set
     */
    public void setListModel(ThreadedListModel listModel) {
        this.listModel = listModel;
    }


    /**
     * @return the listAvailableGroups
     */
    public JList getListAvailableGroups() {
        return listAvailableGroups;
    }


    /**
     * @param listAvailableGroups the listAvailableGroups to set
     */
    public void setListAvailableGroups(JList listAvailableGroups) {
        this.listAvailableGroups = listAvailableGroups;
    }
    
    
    


}
